<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>门店管理</title>
    <meta name="keywords" content="H+后台主题,后台bootstrap框架,会员中心主题,后台HTML,响应式后台">
    <meta name="description" content="H+是一个完全响应式，基于Bootstrap3最新版本开发的扁平化主题，她采用了主流的左右两栏式布局，使用了Html5+CSS3等现代技术">
    <link rel="shortcut icon" href="favicon.ico">
	<link href="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="<%=basePath%>hplus/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="<%=basePath%>hplus/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="<%=basePath%>hplus/css/animate.min.css" rel="stylesheet">
    <link href="<%=basePath%>hplus/css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="<%=path%>/css/marketing/public.css" /> 
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content">
        <div class="ibox-content">
             <span class="text-navy" style="font-size:22px;" id="count"></span>个门店
             <div class="col-sm-3" style="float:right;min-width:350px;">
                <div class="col-sm-2 input-group" style="float: right;">
                    <input type="text" id="bm_keyword" placeholder="请填写电话或门店名称进行查找" class="input form-control" style="width:240px;">
                    <span class="input-group-btn">
                        <button id="bm_csous" onclick="javascript:getStoreList();" type="button" class="btn btn btn-primary"> <i class="fa fa-search"></i> 搜索</button>
                </div>
            </div>
        </div>
        <div class="col-sm-12 text-right" style="p_storeing:16px 0;margin-top: 10px;margin-bottom: 10px;"> 
        	<shiro:hasPermission name="store:create">
            <button id="add_store" class="btn btn-primary" type="button" >新增门店</button>
            </shiro:hasPermission>
        </div>
        <!-- 显示列表 -->
        <div id="tbody" style="float:left; width:100%"></div>
        <!--  -->
        <div class="m_xpages" style="width:100%;margin:0;padding: 2px 0 1px 0;">
				<div class="mian" style="margin-left: 400px;">
					<span>显示行数</span> <select><option>8</option>
						<option>16</option>
						</select> <span>共0条</span> <span>1/1页</span> <a
						href="javascript:;" class="prev">上一页</a> <a href="javascript:;"
						class="next">下一页</a> <span>前往</span> <input type="text" value="1"
						class="text"> <span>页</span>
				</div>
			</div>
    </div>


	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
	<script src="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="<%=basePath%>hplus/js/content.min.js?v=1.0.0"></script>

    <script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>
	<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
	<script src="<%=path%>/hplus/js/plugins/sweetalert/sweetalert2.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/artDialog/7.0.0/dialog-plus.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/artDialog/7.0.0/dialog-plus.js"></script>
	<script type="text/javascript">
   var access_token=$.cookie('access_token');
   var cid=$.cookie('cid');
   var uid=$.cookie('uid');
   var address="";
   var name="";
   var phone="";
   var key_word="";


	//分页
   var startIndex = 0;
   var pageSize = 8;
   var currentPage = 1;
   var pageCount = 0;
   
   $(function(){
	   getCount();
	   getStoreList();
   });
   
   //查询所有门店
   function getStoreList(){
   	//获取关键字
   	key_word = $('#bm_keyword').val();
   	var url = "<%=path%>/store/ajaxStorePageList";
   	$.post(url,{
   		"access_token" : access_token,
			"cid" : cid,
			"uid" : uid,
			"keyWord" : key_word,
			"startIndex" : startIndex,
			"pageSize" : pageSize ,
   	},function(data){
   		//console.log(data);
   		var datas=data.data;
   		if(data.status == "200"){
   			page(data.totalCount);
   			storeHtml(data.data);
   		}else if(data.status == "300"){
   			storeHtml(0);
   			//errorDialog('您还未添加任何门店哦',"");
   		}
   		
   	});
   } 
    //删除门店
    function deleteStore(){
    	var url = "<%=path%>/store/ajaxDeleteStore";
    	var lineId = '#line'+storeeId;
    	var flag=confirm("删除门店后，将无法查看门店相关信息，确定删除吗？");
    	if(flag){
    	$.post(url,{
    		"access_token" : access_token,
			"cid" : cid,
			"uid" : uid,
			"id" : storeeId,
    	},function(data){
    		//var datas = JSON.parse(data);
    		
    		if(data.status == "200"){
    			$(lineId).remove();
    			func_artdialog("提示","删除成功");
    			window.location.reload();
    		}
    		else if(data.status == "500"){
    			func_artdialog("提示","删除失败");
    		}else{
    			func_artdialog("提示","系统出错");
    		}
    	});
    	}
    }   
    function storeHtml(data){
    	var html = "";
    	var phone="";
    	for(var i=0;i<data.length;i++){
    		phone="";
    		if(StringEmpty(data[i].phone)){
    			phone=data[i].phone;
    		}
	    	html+='<div class="col-sm-3" style="padding-left:0;"id=line'+data[i].id+'><div class="contact-box" ><h3><a class="editor_store" style="color:#676A6C" href="javascript:void(0)"';
	    	html+=' sid='+data[i].id+' cname='+data[i].name+' caddress='+data[i].address+' cphone='+phone+' ><strong style="line-height:25px">';
	    	html+=data[i].name+'</strong></a></h3>';
	    	//html+=data[i].name+'</strong></a><a class="close-link" style="float:right;color: #000;" id="close_store" cid='+data[i].id+' href="javascript:void(0)"><i class="fa fa-times"></i></a></h3>';
	    	//html+='<p>编号：'+data[i].id+'</p>'+'<p>电话：';
	    	html+='<p>电话：';
	    	html+=phone+'</p><p style="white-space:nowrap; text-overflow:ellipsis; overflow:hidden;">地址：';
	    	html+=data[i].address+'</p><div class="clearfix"></div></div></div>';
    	}
    	$('#tbody').html(html);
     } 
    /* 分页 */
    $(".m_xpages select").change(function(){
 		pageSize = Number($(this).val());		
 		getStoreList();
 	});
 	
    $(".m_xpages a.prev").click(function(){	 		
 		$('.next').html('<font color="black">下一页</font>');
 		if(currentPage > 1){
 			currentPage--;
 			startIndex -= pageSize;	 
 			if(currentPage == 1){
	 			$('.prev').html('<font color="#E0E0E0">上一页</font>');
 			}
	 			getStoreList();
 		}
 	});
 	$(".m_xpages a.next").click(function(){
 		$('.prev').html('<font color="black">上一页</font>'); 
 		if(currentPage < pageCount){
 			currentPage++;
 			startIndex += pageSize;
 			if(currentPage == pageCount){
	 			$('.next').html('<font color="#E0E0E0">下一页</font>');
	 		}
	 			getStoreList();
 		}
 	});
 	
 	$(".m_xpages :text").keyup(function(e){
		if(e.keyCode==13){
			var pageGo = $(this).val();
			if(/^[0-9]+$/.test(pageGo)){
				pageGo = Number(pageGo);
				if(pageGo<1 || pageGo>pageCount){
					func_artdialog("提示",'页数不存在！');
				}else if(pageGo != currentPage){
					startIndex = (pageGo -1) * pageSize ;	
					getStoreList();
				}
			}else{
				func_artdialog("提示","请输入合法页数！");
			}
		}
	});
 	function func_artdialog(name,value){
 		var d = dialog({
 			title: name,
 			fixed: true,
 			width: 200,
 	        height:100,
 			content:"<center style='font-size:20px; line-height:100px;'>"+value+"</center>",
 			okValue: "确定",
 			ok: function () {
 				return true;
 			}
 		});
 		d.show();
 	}
 	function page(totalCount) {
		pageCount = totalCount/pageSize;
		var count=totalCount % pageSize;
 		if (count>0){
 			pageCount++;
 		}
 		pageCount = parseInt(pageCount);
 		currentPage =  startIndex / pageSize + 1;
 		currentPage = parseInt(currentPage);
 		
 		if(currentPage == 1){
 			$(".prev").addClass("pageDisable");
 		}else{
 			$(".prev").removeClass("pageDisable");
 		}
 		
 		if(currentPage == pageCount || pageCount == 0){
 			$(".next").addClass("pageDisable");
 		}else{
 			$(".next").removeClass("pageDisable");
 		}
 		
 		$(".m_xpages>.mian>span:eq(1)").text('共'+totalCount+'条');
 		$(".m_xpages>.mian>span:eq(2)").text(currentPage+'/'+pageCount+'页');
	}
    
    //判断字符串是否为空
	function StringEmpty(content) {
		if (content == undefined || content == "" || content == null) {
			return false;
		} else {
			return true;
		}
	}
	function getCount(){
		$.ajax({
  		  url: "${pageContext.request.contextPath}/store/ajaxStoreCount",

  		  type: "post",
  		  
  		  async: true,

  		  dataType: "json",

  		  timeout: 1000,

  		  error: function(){},

  		  success: function(data){
  			  if(data.errro_code="0"){
  				  $("#count").text(data.data);
  			  }else{
  				  func_artdialog("提示",data.message);
  			  }
  			 
  		  }                 
  		  });
	}
</script>
<script type="text/javascript">




//删除门店
$(document).on('click','#close_store',function(){
    storeeId = $(this).attr("cid");
    <shiro:hasPermission name="store:delete">
	deleteStore();
	</shiro:hasPermission>
}); 

$('#add_store').click(function(){
	window.location.href="${pageContext.request.contextPath}/view/ui/setStore?flag=1";
});
//编辑时获取门店id 
$(document).on('click','.editor_store',function(){
	storeId = $(this).attr("sid");
	//console.log("------id"+storeId);
	<shiro:hasPermission name="store:update">
	window.location.href="${pageContext.request.contextPath}/view/ui/setStore?flag=2&id="+storeId;
	</shiro:hasPermission>
});

</script> 
    <script type="text/javascript">
    	
    </script>
</body>
</html>
